/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller.deprecated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.deprecated.REDealType;
import org.cx1.model.deprecated.RETypeL1;
import org.cx1.model.deprecated.RETypeL2;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.deprecated.IRETypeService;
import org.cx1.service.impl.deprecated.RETypeService;
import org.cx1.web.controller.action.deprecated.RETypeAction;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class RETypeController extends AbstractController {
    
    private IRETypeService reTypesService = new RETypeService();
    
    public RETypeController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        /*
        String userID = SessionUtils.getUserID(request.getSession());
        if (StringUtils.isEmpty(userID)) {
            mav.setView( new RedirectView("login.htm") );
            return mav;
        }
        
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            case RETypeAction.RE_TYPES_L1_VIEW_ALL: {
                getRETypeL1All(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_L1_VIEW: {
                getRETypeL1(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_L1_CREATE: {
                createRETypeL1(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_L1_DEACTIVATE: {
                break;
            }
            case RETypeAction.RE_TYPES_L2_VIEW_ALL: {
                getRETypeL2All(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_L2_VIEW: {
                getRETypeL2(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_L2_CREATE: {
                createRETypeL2(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_L2_DEACTIVATE: {
                break;
            }
            case RETypeAction.RE_TYPES_DEAL_VIEW_ALL: {
                getREDealTypeAll(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_DEAL_VIEW: {
                getREDealType(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_DEAL_CREATE: {
                createREDealType(mav, map, request);
                break;
            }
            case RETypeAction.RE_TYPES_DEAL_DEACTIVATE: {
                break;
            }       
            default: {
                break;
            }
        }        
        */
        
        mav.addObject("model", map);
        return mav;
    }
    
    /*
    private void getRETypeL1All(ModelAndView mav, Map map, HttpServletRequest request) {
        ServiceResponse<List<RETypeL1>> response = reTypesService.getRETypeL1All();
        map.put("types", response.getResponse());
        mav.setViewName("retypel1all");
    }
     
    private void getRETypeL1(ModelAndView mav, Map map, HttpServletRequest request) {
        String id = request.getParameter("id");
        ServiceResponse<RETypeL1> response = reTypesService.getRETypeL1(id);
        map.put("type", response.getResponse());
        mav.setViewName("retypel1");
    }
    
    private void createRETypeL1(ModelAndView mav, Map map, HttpServletRequest request) {
        if (!StringUtils.isEmpty(request.getParameter("s"))) {            
            String title = request.getParameter("title");
            String desc = request.getParameter("desc");

            ServiceResponse<String> response = reTypesService.addRETypeL1(title, desc);        
            mav.setView( new RedirectView("retype.htm?a=" + RETypeAction.RE_TYPES_L1_VIEW_ALL) );
        }
        else {
            mav.setViewName("retypel1add");
        }
    }
    
    private void getRETypeL2All(ModelAndView mav, Map map, HttpServletRequest request) {
        ServiceResponse<List<RETypeL2>> response = reTypesService.getRETypeL2All();
        map.put("types", response.getResponse());
        mav.setViewName("retypel2all");
    }        
    
    private void getRETypeL2(ModelAndView mav, Map map, HttpServletRequest request) {
        String id = request.getParameter("id");
        ServiceResponse<RETypeL2> response = reTypesService.getRETypeL2(id);
        map.put("type", response.getResponse());
        mav.setViewName("retypel1");
    }
    
    private void createRETypeL2(ModelAndView mav, Map map, HttpServletRequest request) {
        if (!StringUtils.isEmpty(request.getParameter("s"))) {            
            String title = request.getParameter("title");
            String desc = request.getParameter("desc");

            ServiceResponse<String> response = reTypesService.addRETypeL2(title, desc);        
            mav.setView( new RedirectView("retype.htm?a=" + RETypeAction.RE_TYPES_L2_VIEW_ALL) );
        }
        else {
            mav.setViewName("retypel2add");
        }
    }
    
    private void getREDealTypeAll(ModelAndView mav, Map map, HttpServletRequest request) {
        ServiceResponse<List<REDealType>> response = reTypesService.getREDealTypeAll();
        map.put("types", response.getResponse());
        mav.setViewName("retypedealall");
    }
        
    
    private void getREDealType(ModelAndView mav, Map map, HttpServletRequest request) {
        String id = request.getParameter("id");
        ServiceResponse<REDealType> response = reTypesService.getREDealType(id);
        map.put("type", response.getResponse());
        mav.setViewName("retypel1");
    }
    
    private void createREDealType(ModelAndView mav, Map map, HttpServletRequest request) {
        if (!StringUtils.isEmpty(request.getParameter("s"))) {            
            String title = request.getParameter("title");
            String desc = request.getParameter("desc");

            ServiceResponse<String> response = reTypesService.addREDealType(title, desc);        
            mav.setView( new RedirectView("retype.htm?a=" + RETypeAction.RE_TYPES_DEAL_VIEW_ALL) );
        }
        else {
            mav.setViewName("retypedealadd");
        }
    }
    */
}
